﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using ControleRepresentante.Logic.Models;
using WebSite.ViewModels.EditViewModels.EditGridViewModels;
using WebSite.ViewModels.Fields;

namespace WebSite.ViewModels.EditViewModels
{
    public class ClienteEditViewModel : DetailsHolderEditViewModel
    {
        private readonly Dictionary<Guid, string> m_Vendedores;

        public ClienteEditViewModel(Dictionary<Guid, string> vendedores)
            : this()
        {
            m_Vendedores = vendedores;
        }

        public ClienteEditViewModel(string message = null)
            : base("Cliente", message)
        {
        }

        protected override IEnumerable<IField> GetGeralFields()
        {
            Cliente cliente = (Cliente)Model;

            DropDownListField vendedorField = new DropDownListField("Vendedor", "vendedorId", true);
            vendedorField.AddEmpty(cliente.Vendedor == null, "Nenhum");
            foreach (KeyValuePair<Guid, string> vendedor in m_Vendedores)
            {
                vendedorField.Add(vendedor.Value, vendedor.Key,
                                  cliente.Vendedor != null && (cliente.Vendedor.Id == vendedor.Key));
            }

            List<IField> result = new List<IField>
            {
                new TextBoxField("Razão Social", "razaoSocial", cliente.RazaoSocial, true),
                new TextBoxField("Nome Fantasia", "nomeFantasia", cliente.NomeFantasia, true),
                new TextBoxField("CNPJ", "cnpj", cliente.CNPJ, true),
                new TextBoxField("Inscrição Estadual", "inscricaoEstadual", cliente.InscricaoEstadual, true),
                vendedorField,
                new DatePickerField("Cliente Desde", "clienteDesde", cliente.ClienteDesde)
            };
            result.AddRange(base.GetGeralFields());
            return result;
        }

        protected override IEditGridViewModel GetEnderecoEditGridViewModel(HtmlHelper htmlHelper, DetailsHolder detailsHolder)
        {
            Cliente cliente = (Cliente)detailsHolder;
            IEnumerable<ClienteEnderecoViewModel> enderecoViewModels = cliente.Enderecos
                .Select(x => new ClienteEnderecoViewModel(x, cliente));
            return new ClienteEnderecoEditGridViewModel(htmlHelper, ControllerName, enderecoViewModels);
        }
    }
}